﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginFramework.Common;
using QLBH.Model.Controller;
using QLBH.StandardUI;
using System.Windows.Forms;
using QLBH.DataModel;
using DevComponents.DotNetBar;
using QLBH.FeatureBase.Modules.Category;
using QLBH.FeatureBase;
using QLBH.Base;

namespace QLBH.Category
{
    [ExtensionOf(typeof(QLDVTToolExtensionPoint))]
    public class QLDVTTool:DialogBase,IQLDVTTool
    {
        public QLDVTTool()
            :base()
        {
            InitializeComponent();
        }
        public DialogBase GetDialog()
        {
            return new QLDVTTool();
        }
        public override void FormLoaded(object sender, EventArgs e)
        {
            base.FormLoaded(sender, e);
        }
        public override void AddNew(object sender, EventArgs e)
        {
            base.AddNew(sender, e);
            AddDVTDlg addNew = new AddDVTDlg();
            if (addNew.ShowDialog()==DialogResult.OK)
            {
                RefreshData(sender, e);
            } 
           
        }
        public override void Edit(object sender, EventArgs e)
        {
            AddDVTDlg editItem = new AddDVTDlg();
            editItem.DlgType = eDlgType.EDIT;
            var model = DataGridView.SelectedRows[0].DataBoundItem as DONVITINH;

            editItem.Model = model;
            if (editItem.ShowDialog() == DialogResult.OK)
            {
                RefreshData(sender, e);
            } 
            base.Edit(sender, e);
        }
        public override void Delete(object sender, EventArgs e)
        {
            try
            {
                var model = DataGridView.SelectedRows[0].DataBoundItem as DONVITINH;
                DVTController.GetInstance().Delete(model);
                RefreshData(sender, e);

                base.Delete(sender, e);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }
            
            
        }
        public override void RefreshData(object sender, EventArgs e)
        {
            try
            {
                base.RefreshData(sender, e);

                var items = DVTController.GetInstance().GetAll().ToList();
                DataSource = items;
                DataGridView.DataSource = DataSource;
                foreach (DataGridViewColumn col in DataGridView.Columns)
                {
                    if (col.Name == "MADVT")
                    {
                        col.HeaderText = "Mã ĐVT";
                    }
                    else if (col.Name == "TENDVT")
                    {
                        col.HeaderText = "Tên ĐVT";
                    }
                    else if (col.Name == "GHICHU")
                    {
                        col.HeaderText = "Ghi Chú";
                    }
                    else
                    {
                        col.Visible = false;
                    }
                }

                if (items.Count() == 0)
                {
                    ShowMessage.Instance.ShowInfo("Không có ĐVT trong database");
                }
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }
            
        }
        public override void SaveData(object sender, EventArgs e)
        {
            base.SaveData(sender, e);
        }
        public override void BuildSearchMenu()
        {
            ButtonItem btnSearchbyID = new ButtonItem("btnSearchByID", "Tìm theo mã đvt");
            ButtonItem btnSearchByName = new ButtonItem("btnSearchByName", "Tìm theo tên đvt");

            btnSearchbyID.Click += btnSearchbyID_Click;

            btnSearchByName.Click += btnSearchByName_Click;

            BtnTimKiem.SubItems.Add(btnSearchbyID);
            BtnTimKiem.SubItems.Add(btnSearchByName);
            base.BuildSearchMenu();
        }

        void btnSearchByName_Click(object sender, EventArgs e)
        {
            List<DONVITINH> source = (List<DONVITINH>)DataSource;

            source = source.Where(m => m.TENDVT.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        void btnSearchbyID_Click(object sender, EventArgs e)
        {
            List<DONVITINH> source = (List<DONVITINH>)DataSource;

            source = source.Where(m => m.MADVT.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // QLDVTTool
            // 
            this.ClientSize = new System.Drawing.Size(685, 485);
            this.Name = "QLDVTTool";
            this.ResumeLayout(false);

        }

        
    }
    
}
